package com.glorious.security.core.userdetails.user;

import com.glorious.common.enums.PasswordEncoderTypeEnum;
import com.glorious.constant.GlobalConstants;
import com.glorious.dto.response.appuser.AppUserAuthResponse;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;


/**
 * 用户认证信息
 */
@Data
public class AppUserDetails implements UserDetails {

    private Long userId;
    private String username;
    private String password;
    private Boolean enabled;

    /**
     * 认证方式
     */
    private String authenticationMethod;


    /**
     * 小程序会员用户体系
     *
     * @param userResponse 小程序会员用户认证信息
     */
    public AppUserDetails(AppUserAuthResponse userResponse) {
        this.setUserId(userResponse.getUserId());
        this.setUsername(userResponse.getUsername());
        this.setPassword(PasswordEncoderTypeEnum.BCRYPT.getPrefix() + userResponse.getPassword());
        this.setEnabled(GlobalConstants.STATUS_YES.equals(userResponse.getStatus()));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> collection = new HashSet<>();
        return collection;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
